package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Log;
import com.victor.entity.city.CityIntroduce;
import com.victor.service.CityService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 后台城市管理
 * 
 * @author qinyuquan
 *
 */
@RestController
@RequestMapping("/city")
public class CityController {

	@Autowired
	private CityService service;

	/**
	 * 添加城市信息
	 * 
	 * @param cityIntroduce
	 * @return
	 */
	@RequestMapping("addCity")
	public Msg addCity(@RequestBody CityIntroduce cityIntroduce) {
		if (cityIntroduce == null || cityIntroduce.getCityIntroducePhotos().size() == 0) {
			return Msg.fail("参数错误");
		}
		boolean b = service.addCity(cityIntroduce);
		if (!b) {
			return Msg.fail("添加城市失败");
		}
		for (int i = 0; i < cityIntroduce.getCityIntroducePhotos().size(); i++) {
			cityIntroduce.getCityIntroducePhotos().get(i).setC_id(cityIntroduce.getC_id() + "");
			boolean c = service.addCityPhotos(cityIntroduce.getCityIntroducePhotos().get(i));
			if (!c) {
				return Msg.fail("添加城市图片失败");
			}
		}
		return Msg.judge(b);
	}

	/**
	 * 通过城市id修改城市信息
	 * 
	 * @param cityIntroduce
	 * @return
	 */
	@RequestMapping("updateCity")
	public Msg updateCityById(@RequestBody CityIntroduce cityIntroduce) {
		if (cityIntroduce == null || cityIntroduce.getC_id() == null) {
			return Msg.fail("参数错误");
		}
		boolean b = service.updateCityById(cityIntroduce);
		if (!b) {
			return Msg.fail("修改城市失败");
		}
		if (cityIntroduce.getCityIntroducePhotos() != null) {
			for (int i = 0; i < cityIntroduce.getCityIntroducePhotos().size(); i++) {
				cityIntroduce.getCityIntroducePhotos().get(i).setC_id(cityIntroduce.getC_id() + "");
				boolean c = service.updateCityPhotos(cityIntroduce.getCityIntroducePhotos().get(i));
				if (!c) {
					return Msg.fail("修改城市图片失败");
				}
			}
		}
		return Msg.judge(b);
	}

	/**
	 * 根据省份查询城市列表(省份可为' ' )
	 * 
	 * @param cityIntroduce
	 * @return
	 */
	@RequestMapping("selectCityIntroduce")
	public Msg selectCityIntroduce(String c_inprovince) {
		List<CityIntroduce> list = service.selectCityIntroduce(c_inprovince);
		return Msg.success().add("list", list);
	}

	/**
	 * 后台批量删除城市信息和图片
	 * 
	 * @param c_id
	 * @return
	 */
	@RequestMapping("batchDelete")
	public Msg batchDelete(int[] c_id) {
		if (c_id == null || c_id.length == 0) {
			return Msg.fail();
		}
		boolean b = service.batchDelete(c_id);
		if (!b) {
			return Msg.fail("删除城市失败");
		}
		for (int id : c_id) {

			boolean c = service.batchDeletePhoto(id + "");
			if (!c) {
				return Msg.fail("删除城市照片失败");
			}
		}
		return Msg.success();
	}

	/**
	 * 导出所有城市列表(添加操作日志)
	 */
	@RequestMapping("batchExport")
	public void batchExport(String adminid, HttpServletResponse response, HttpServletRequest request,
			String c_inprovince) throws Exception {
		// 数据库查询数据 exportlist 需要导出的list
		List<CityIntroduce> cityIntroduces = service.selectCityIntroduce(c_inprovince);

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("城市介绍列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("城市介绍列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("城市id编号");
		headRow.createCell(1).setCellValue("城市名称");
		headRow.createCell(2).setCellValue("核心介绍");
		headRow.createCell(3).setCellValue("详情内容");

		// 遍历上面数据库查到的数据
		for (CityIntroduce city : cityIntroduces) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(city.getC_id());
			dataRow.createCell(1).setCellValue(city.getC_name());
			dataRow.createCell(2).setCellValue(city.getC_coreintroduce());
			dataRow.createCell(3).setCellValue(city.getC_introduce());

		}
		// 下载导出文件名
		String filename = "城市介绍列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

		// 添加操作日志
		Log log = new Log();
		log.setDate(DateUtils.getNowDate());
		log.setTime(DateUtils.getNowTime());
		log.setAdminid(adminid);
		log.setContent("批量导出城市介绍列表");
	}
}
